package com.yikes.yygh.config;

import com.yikes.yygh.config.properties.SmsProperties;
import com.yikes.yygh.redis.constant.SmsConstant;
import com.yikes.yygh.utils.HttpSmsUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
/**
 * <p>
 *
 * </p>
 *
 * @Author: guanrong.yin
 * @Date: 2024/12/17 14:42
 * @Version: 1.0
 */
@Component
public class AlibabaSmsConfiguration {
    public void sendSmsCode(String phone, String code) {

        String host = SmsProperties.HOST;
        String path = SmsProperties.PATH;
        String method = "POST";
        String appcode = SmsProperties.APP_CODE;

        Map<String, String> headers = new HashMap<String, String>();

        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);

        Map<String, String> querys = new HashMap<>();
        querys.put("mobile", phone);

        querys.put("param", "**code**:" + code + "," + "**minute**:" + SmsConstant.NUMERAL_MINUTE);

        querys.put("smsSignId", SmsProperties.SMS_SIGN_ID);
        querys.put("templateId", SmsProperties.TEMPLATE_ID);

        Map<String, String> bodys = new HashMap<>();


        try {

            HttpResponse response = HttpSmsUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());

            // 获取response的body
            System.out.println(EntityUtils.toString(response.getEntity()));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
